package myplaydog.qf.com.yimixianproject.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import myplaydog.qf.com.yimixianproject.R;
import myplaydog.qf.com.yimixianproject.fragment.AccountFragment;
import myplaydog.qf.com.yimixianproject.fragment.BaseFragment;
import myplaydog.qf.com.yimixianproject.fragment.HomeFragment;
import myplaydog.qf.com.yimixianproject.fragment.KindFragment;
import myplaydog.qf.com.yimixianproject.fragment.OrderFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private FragmentManager manager;
    private List<BaseFragment> mList;
    private RadioGroup mRadioGroup;
    //退出时间
    private long currentBackPressedTime = 0;
    //间隔时间
    private static final int BACK_PRESSED_INTERVAL = 2000;
    private ImageView mImageVIew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initFragment();
        setData();
        setListener();
    }
    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(this);
        mImageVIew.setOnClickListener(this);
    }

    private void setData() {

        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.activity_main_fragmentId, mList.get(0));
        transaction.commit();
    }

    private void initFragment() {

        HomeFragment homeFragment = new HomeFragment();
        mList.add(homeFragment);
        KindFragment kindFragment =new KindFragment();
        mList.add(kindFragment);
        OrderFragment orderFragment =new OrderFragment();
        mList.add(orderFragment);
        AccountFragment accountFragment =new AccountFragment();
        mList.add(accountFragment);


    }

    private void initData() {

        manager = getSupportFragmentManager();
        mList = new ArrayList<>();
    }

    private void initView() {
        mImageVIew = ((ImageView) findViewById(R.id.activitiy_main_iv));
        mRadioGroup = ((RadioGroup) findViewById(R.id.activity_main_radiogroupId));
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
            currentBackPressedTime = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再次按键退出程序", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = manager.beginTransaction();
        switch (checkedId) {
            case R.id.activity_main_rb_home:
                transaction.replace(R.id.activity_main_fragmentId, mList.get(0));
                break;
            case R.id.activity_main_rb_kind:
                transaction.replace(R.id.activity_main_fragmentId, mList.get(1));
                break;
            case R.id.activity_main_rb_order:
                transaction.replace(R.id.activity_main_fragmentId, mList.get(2));
                break;
            case R.id.activity_main_rb_account:
                transaction.replace(R.id.activity_main_fragmentId, mList.get(3));
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(this,CartActivity.class));
        overridePendingTransition(R.anim.activity_open,0);
    }

    public void ViewClick(View view){
        if (view!=null){
            switch (view.getId()){
                case R.id.fragment_account_denglu:
                    startActivity(new Intent(this,LandingActivity.class));
                    overridePendingTransition(R.anim.activity_open,0);
                    break;
                case R.id.fragment_account_setting:
                    startActivity(new Intent(this,SettingActivity.class));
                    overridePendingTransition(R.anim.activity_open,0);
                    break;

            }
        }
    }
}
